<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>
    <script src="validate/jquery-1.11.0.min.js"></script>
    <script src="validate/jquery.validator.min.js"></script>
    <script src="validate/local/zh-CN.js"></script>
    <link href="validate/jquery.validator.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>
    <form method="post" id="form" action="${pageContext.request.contextPath}/userServlet?method=deleteSelected">
        <table border="1" class="table table-bordered table-hover">
            <tr>
                <td colspan="9" align="right">
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a>
                    <a class="btn btn-primary" href="javascript:void(0)" onclick="delsel()">删除选中</a>
                </td>
            </tr>
            <tr class="success">
                <th><input type="checkbox" onclick="checkAll(this.checked)"></th>
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>籍贯</th>
                <th>QQ</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${page.list}" var="user" varStatus="s">
                <tr>
                    <td><input type="checkbox" name="uid" value="${user.id}" class="cc"></td>
                    <td>${s.count}</td>
                    <td>${user.name}</td>
                    <td>${user.gender}</td>
                    <td>${user.age}</td>
                    <td>${user.address}</td>
                    <td>${user.qq}</td>
                    <td>${user.email}</td>
                    <td><a class="btn btn-default btn-sm"
                           href="${pageContext.request.contextPath}/userServlet?method=ById&id=${user.id}">修改</a>&nbsp;
                        <a class="btn btn-default btn-sm"
                           href="${pageContext.request.contextPath}/userServlet?method=delete&id=${user.id}"
                           onclick="return del()">删除</a></td>
                </tr>
            </c:forEach>

        </table>
    </form>
</div>
<div>
    <nav aria-label="Page navigation">
        <ul class="pagination">

            <li>
                <a href="${pageContext.request.contextPath}/userServlet?method=findByPage&currentPage=1&pageSize=${page.pageSize}"
                   aria-label="Previous">
                    <span aria-hidden="true">首页</span>
                </a>
            </li>
            <li>
                <a href="${pageContext.request.contextPath}/userServlet?method=findByPage&currentPage=${page.pageNum-1}&pageSize=${page.pageSize}"
                   aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>

            <c:forEach begin="1" end="${page.pages}" var="i">
                <li ${page.pageNum == i ? "class='active'" : ""}>
                    <a href="${pageContext.request.contextPath}/userServlet?method=findByPage&currentPage=${i}&pageSize=${page.pageSize}">${i}</a>
                </li>
            </c:forEach>

            <li>
                <a href="${pageContext.request.contextPath}/userServlet?method=findByPage&currentPage=${page.pageNum+1}&pageSize=${page.pageSize}"
                   aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>

            <li>
                <a href="${pageContext.request.contextPath}/userServlet?method=findByPage&currentPage=${page.pages}&pageSize=${page.pageSize}"
                   aria-label="Next">
                    <span aria-hidden="true">末页</span>
                </a>
            </li>


            <span style="font-size: 25px;margin-left: 5px;">
                    共${page.total}条记录，共${page.pages}页
                </span>

        </ul>
    </nav>


</div>
</body>
<script>
    //全选和全不选
/*    document.getElementById("first").onclick = function () {
        var flag = this.checked;
        var className = document.getElementsByClassName("cc");
        for (var i = 0; i < className.length; i++) {
            className[i].checked = flag;
        }
    }*/
    //JQuery方式  全选全不选
    function checkAll(flag) {
        $(".cc").prop("checked",flag);
    };


    //删除单个
    function del() {
        var flag = confirm("确认删除不?")
        if (flag) {
            return true;
        } else {
            return false;
        }
    }

    // 删除选中
/*    function delsel() {
        var chks = document.getElementsByName("uid");
        var flag = false;
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked == true) {
                flag = true;
            }
        }
        if (!flag) {
            alert("请勾选你要删除的数据!!");
            return false;
        }

        if (confirm("您确认要删除选中吗??")) {
            document.getElementById("form").submit();
        }

    }*/
    function delsel() {
        let len = $("input[name='uid']:checked").length;
        if(len<1){
            alert("请勾选你要删除的数据!!");
            return;
        }
        if (confirm("您确认要删除选中吗??")) {
            $("#form").submit();
        }
    }

</script>
</html>
